#include "StdAfx.h"
#include "LocalController.h"
#include "Actor.h"
#include "Weapon.h"
#include "Engine.h"
//=>GAME

//<=

//=>Physics
#include "PhysicsPropertyMessages.h"
//<=

namespace _MODULE_
{
	namespace _GAME_
	{

		LocalController::LocalController(void)
		{
			ZeroMemory(&m_GameControllCommandStates,sizeof(m_GameControllCommandStates));
			ZeroMemory(&m_OldGameControllCommandStates,sizeof(m_OldGameControllCommandStates));
		}
		LocalController::~LocalController(void)
		{
		}


		bool LocalController::ProcessCommand(const _FOGGE_::foString& command,const _FOGGE_::foString& flag )
		{
			
			
			if(flag==_T("Normal"))
			{
			
				return false;
			}
			else 
			{
				bool keydown=false;
				if(flag==_T("Start"))
					keydown=true;
				else
					keydown=false;

			
				if(command ==_T("MoveUp"))
				{
					m_GameControllCommandStates.MoveFlags.bMoveForward=keydown;
				}
				else if(command==_T("MoveDown"))
				{
					m_GameControllCommandStates.MoveFlags.bMoveBackward=keydown;
				}
				else if(command==_T("MoveLeft"))
				{
					m_GameControllCommandStates.MoveFlags.bMoveLeft=keydown;
				}
				else if(command==_T("MoveRight"))
				{
					m_GameControllCommandStates.MoveFlags.bMoveRight=keydown;
				}
				else if(command==_T("RotatePitchCCD"))
				{
					m_GameControllCommandStates.RotateFlags.bRotatePitchCCW=keydown;
				}
				else if(command==_T("RotatePitchCD"))
				{
					m_GameControllCommandStates.RotateFlags.bRotatePitchCW=keydown;
				}
				else if(command==_T("RotateYawCCD"))
				{
					m_GameControllCommandStates.RotateFlags.bRotateYawCCW=keydown;
				}
				else if(command==_T("RotateYawCD"))
				{
					m_GameControllCommandStates.RotateFlags.bRotateYawCW=keydown;
				}
				else if(command==_T("RotateRollCCD"))
				{
					m_GameControllCommandStates.RotateFlags.bRotateRollCCW=keydown;
				}
				else if(command==_T("RotateRollCD"))
				{
					m_GameControllCommandStates.RotateFlags.bRotateRollCW=keydown;
				}
				else if(command==_T("FirePrimaryWeapon"))
				{
					m_GameControllCommandStates.ControllFlags.bControllPrimaryWeapon=keydown;
				}
				else
					return false;
			}

			
			
			return true;
		}

		void LocalController::Update(float elapsedtime)
		{

			if(m_GameControllCommandStates.MoveFlags.Flag!=m_OldGameControllCommandStates.MoveFlags.Flag)
			{
				m_OldGameControllCommandStates.MoveFlags.Flag=m_GameControllCommandStates.MoveFlags.Flag;
				
				_PMMSG::PMM_ChangeVelocity *ptrCV=FoDynamicCast(_PMMSG::PMM_ChangeVelocity,GetNewMassage(_PMMSG::ePMM_ChangeVelocity ));

				ptrCV->m_ptrObject=m_ptrControllingActor->GetOwner();

				_FOMATH::Vector result(0,0,0);

				
				//moves
				if(m_GameControllCommandStates.MoveFlags.bMoveForward)
				{
					result+=_FOMATH::DIRECTION_FRONT;
				}
				else if(m_GameControllCommandStates.MoveFlags.bMoveBackward)
				{
					result-=_FOMATH::DIRECTION_FRONT;
				}

				if(m_GameControllCommandStates.MoveFlags.bMoveRight)
				{
					result+=_FOMATH::DIRECTION_RIGHT;
				}
				else if(m_GameControllCommandStates.MoveFlags.bMoveLeft)
				{
					result-=_FOMATH::DIRECTION_RIGHT;
				}


				if(m_GameControllCommandStates.MoveFlags.bMoveUp)
				{
					result+=_FOMATH::DIRECTION_UP;
				}
				else if(m_GameControllCommandStates.MoveFlags.bMoveDown)
				{
					result-=_FOMATH::DIRECTION_UP;
				}

				
			
				ptrCV->m_vVelocity=result*m_ptrControllingActor->GetEngine()->m_fSpeed*100;//speed unit is meter
					
			
				ptrCV->PushReceiver(_FOGGE_::eEMID_Physics);
				SendMessage(ptrCV);
			}
			if(m_GameControllCommandStates.RotateFlags.Flag!=m_OldGameControllCommandStates.RotateFlags.Flag)
			{
				m_OldGameControllCommandStates.RotateFlags.Flag = m_GameControllCommandStates.RotateFlags.Flag;
				_PMMSG::PMM_ChangeAngularVelocity *ptrCAV=FoDynamicCast(_PMMSG::PMM_ChangeAngularVelocity,GetNewMassage(_PMMSG::ePMM_ChangeAngularVelocity ));

				ptrCAV->m_ptrObject=m_ptrControllingActor->GetOwner();


				
				
				//rotations

				if(m_GameControllCommandStates.RotateFlags.bRotateRollCW)
				{
					ptrCAV->m_rAngularVelocity.m_fRoll-=m_ptrControllingActor->GetEngine()->m_fAngluarSpeed;
				}
				else if(m_GameControllCommandStates.RotateFlags.bRotateRollCCW)
				{
					ptrCAV->m_rAngularVelocity.m_fRoll+=m_ptrControllingActor->GetEngine()->m_fAngluarSpeed;
				}

				if(m_GameControllCommandStates.RotateFlags.bRotatePitchCW)
				{
					ptrCAV->m_rAngularVelocity.m_fPitch-=m_ptrControllingActor->GetEngine()->m_fAngluarSpeed;
				}
				else if(m_GameControllCommandStates.RotateFlags.bRotatePitchCCW)
				{
					ptrCAV->m_rAngularVelocity.m_fPitch+=m_ptrControllingActor->GetEngine()->m_fAngluarSpeed;
				}

				if(m_GameControllCommandStates.RotateFlags.bRotateYawCW)
				{
					ptrCAV->m_rAngularVelocity.m_fYaw-=m_ptrControllingActor->GetEngine()->m_fAngluarSpeed;
				}
				else if(m_GameControllCommandStates.RotateFlags.bRotateYawCCW)
				{
					ptrCAV->m_rAngularVelocity.m_fYaw+=m_ptrControllingActor->GetEngine()->m_fAngluarSpeed;
				}

				ptrCAV->PushReceiver(_FOGGE_::eEMID_Physics);
				SendMessage(ptrCAV);

			}

			if(m_GameControllCommandStates.ControllFlags.Flag != m_OldGameControllCommandStates.ControllFlags.Flag)
			{
				m_OldGameControllCommandStates.ControllFlags.Flag = m_GameControllCommandStates.ControllFlags.Flag;
				if(m_GameControllCommandStates.ControllFlags.bControllPrimaryWeapon)
				{
					m_ptrControllingActor->GetPrimaryWeapon()->FireStart();
				}
				else
				{
					m_ptrControllingActor->GetPrimaryWeapon()->FireEnd();
				}

			/*	if(m_GameControllCommandStates.ControllFlags.bControllSecondaryWeapon)
				{
					m_ptrControllingActor->GetSecondaryWeapon()->FireStart();
				}
				else
				{
					m_ptrControllingActor->GetSecondaryWeapon()->FireEnd();
				}*/
			}
				
		}
	}
}